<?php

/**
 * Created by PhpStorm.
 * User: aa
 * Date: 15/10/19
 * Time: 下午6:41
 */
class Image {

	private $image, $image_type, $image_info;

	public function __construct() {
		require_once 'Dir.php';
	}

	public function load($filename) {
		$this->image_info = $image_info = getimagesize($filename);
		$this->image_type = $image_info[2];
		if ($this->image_type == IMAGETYPE_JPEG) {
			$this->image = imagecreatefromjpeg($filename);
		} elseif ($this->image_type == IMAGETYPE_GIF) {
			$this->image = imagecreatefromgif($filename);
		} elseif ($this->image_type == IMAGETYPE_PNG) {
			$this->image = imagecreatefrompng($filename);
		}
		return $this;
	}

	/**
	 * 将图片存到指定的路径
	 */
	public function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 80, $permissions = null) {
		$dir = dirname($filename);
		if (!is_dir($dir)) {
			$d = new Dir;
			$d->mkdirs(dirname($filename));
		}
		if ($image_type == IMAGETYPE_JPEG) {
			imagejpeg($this->image, $filename, $compression);
		} elseif ($image_type == IMAGETYPE_GIF) {
			imagegif($this->image, $filename, $compression);
		} elseif ($image_type == IMAGETYPE_PNG) {
			imagepng($this->image, $filename, $compression);
		}
		if ($permissions != null) {
			chmod($filename, $permissions);
		}
	}

	/*
	 * 将图片打印到屏幕
	 */

	public function output($image_type = IMAGETYPE_JPEG) {
		if ($image_type == IMAGETYPE_JPEG) {
			imagejpeg($this->image);
		} elseif ($image_type == IMAGETYPE_GIF) {
			imagegif($this->image);
		} elseif ($image_type == IMAGETYPE_PNG) {
			imagepng($this->image);
		}
	}

	public function getWidth() {
		return imagesx($this->image);
	}

	public function getHeight() {
		return imagesy($this->image);
	}

	public function resizeToHeight($height) {
		$ratio = $height / $this->getHeight();
		$width = $this->getWidth() * $ratio;
		$this->resize($width, $height);
		return $this;
	}

	public function resizeToWidth($width) {
		if ($this->getWidth() < $width)
			return $this;
		$ratio = $width / $this->getWidth();
		$height = $this->getheight() * $ratio;
		$this->resize($width, $height);
		return $this;
	}

	/**
	 * 按指定比例进行缩放
	 */
	public function scale($scale) {
		$width = $this->getWidth() * $scale / 100;
		$height = $this->getheight() * $scale / 100;
		$this->resize($width, $height);
		return $this;
	}

	/**
	 * 按尺寸进行裁切
	 */
	function rePositionSize($width, $height) {
		$new_image = imagecreatetruecolor($width, $height);
		imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
		$this->image = $new_image;
		return $this;
	}

	/**
	 * 按比例缩小，从中采取一部分
	 */
	public function resize($width, $height) {
		$new_image = imagecreatetruecolor($width, $height);
		$w = $this->getWidth();
		$h = $this->getHeight();
		$x = 0;
		$y = 0;
		if ($w > $h) {
			$w = $width + $width * ($h - $height) / $height;
			$x = ($this->getWidth() / 2) - ($w / 2);
		} elseif ($h > $w) {
			if ($width > $height) {
				$w = $width + $width * ($h - $height) / $height;
				$x = ($this->getWidth() / 2) - ($w / 2);
			} else {
				$h = $height + $height * ($w - $width) / $width;
				$y = ($this->getHeight() / 2) - ($h / 2);
			}
		}
		imagecopyresampled($new_image, $this->image, 0, 0, $x, $y, $width, $height, $w, $h);
		$this->image = $new_image;
		return $this;
	}

	/**
	 * 生成验证码
	 */
	static public function authCode() {
		$code = '0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$str = '';
		for ($i = 1; $i <= 4; $i++) {
			$rand_num = mt_rand(0, strlen($code) - 1);
			$str .= $code[$rand_num];
		}
		Session::set('authCode', $str);
		//构图
		$im = imagecreate(80, 80);
		$dm = imagecreate(80, 30);
		$color = array(mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
		$im_color = ImageColorAllocate($im, $color[0], $color[1], $color[2]);
		$dm_color = ImageColorAllocate($dm, $color[0], $color[1], $color[2]);
		$im_bgColor = ImageColorAllocate($im, 255, 255, 255);
		$dm_bgColor = ImageColorAllocate($dm, 255, 255, 255);
		imagefill($im, 0, 0, $im_bgColor);
		imagefill($dm, 0, 0, $dm_bgColor);

		//填字
		ImageTTFText($im, 15, 0, 5, 38, $im_color, _ROOT_ . "/core/2.ttf", $str);

		//扭曲
		$n = mt_rand(0, 1);
		$h = $n == 0 ? 13 : -13;
		for ($i = 2; $i <= 70; $i++) {
			imagecopy($dm, $im, $i, 0, $i, round(sin($i / $h) * 5) + 18, 80, 30);
		}

		//干扰
		$sinY = mt_rand(17, 25);
		imageline($dm, mt_rand(40, 70), mt_rand(0, 15), mt_rand(70, 80), mt_rand(0, 20), $im_color);
		imageline($dm, mt_rand(40, 70), mt_rand(0, 10), mt_rand(70, 80), mt_rand(0, 10), $im_color);
		for ($i = 0; $i <= 80; $i++) {
			imagesetpixel($dm, $i, round(sin($i / 2) + $sinY), $im_color);
			imagesetpixel($dm, $i, round(sin($i) + $sinY + mt_rand(-5, 5)), $im_color);
		}
		header("Content-type:image/png");
		imagepng($dm);
		imagedestroy($im);
		imagedestroy($dm);
	}

	/**
	 * 判断验证码是否正确
	 */
	static public function isAuthCode($value) {
		$s = Session::get('sp_authCode');
		Session::del('sp_authCode');
		if (strtolower($value) == strtolower($s))
			return true;
		else
			return false;
	}

}
